package net.jjjshop.shop.controller.templ;


import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.jjjshop.common.enums.PageTypeEnum;
import net.jjjshop.common.vo.page.PageVo;
import net.jjjshop.config.properties.SpringBootJjjProperties;
import net.jjjshop.framework.common.api.ApiResult;
import net.jjjshop.framework.core.pagination.Paging;
import net.jjjshop.framework.log.annotation.OperationLog;
import net.jjjshop.shop.controller.page.diy.DefaultItems;
import net.jjjshop.shop.param.CommonPageParam;
import net.jjjshop.shop.param.store.StoreParam;
import net.jjjshop.shop.param.templ.TemplParam;
import net.jjjshop.shop.service.templ.TemplService;
import net.jjjshop.shop.vo.VoFactory;
import net.jjjshop.shop.vo.store.StoreVo;
import net.jjjshop.shop.vo.templ.TemplVo;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api(value = "index", tags = {"index"})
@RestController
@RequestMapping("/shop/templ/templ")
public class TemplController {

    @Autowired
    private TemplService templService;

    @RequestMapping(value = "/getDefaultTemplData", method = RequestMethod.GET)
    @RequiresPermissions("/page/page/edit")
    @OperationLog(name = "getDefaultTemplData")
    @ApiOperation(value = "getDefaultTemplData", response = String.class)
    public ApiResult<Map<String,Object>> getDefaultTemplData(Integer pageType) throws Exception{
        Map<String,Object> result = new HashMap<>();
        result.put("jsonData", VoFactory.createDefaultTemplVo());
        return ApiResult.ok(result);
    }

    @RequestMapping(value = "/getTemplData", method = RequestMethod.GET)
    @RequiresPermissions("/page/page/edit")
    @OperationLog(name = "getTemplData")
    @ApiOperation(value = "getTemplData", response = String.class)
    public ApiResult<Map<String,Object>> getTemplData(Integer templId) throws Exception{
        TemplVo vo = templService.getTemplData(templId);
        Map<String,Object> result = new HashMap<>();
        result.put("jsonData", vo);
        return ApiResult.ok(result);
    }
    @RequestMapping(value = "/templList", method = RequestMethod.POST)
    @RequiresPermissions("/store/store/index")
    @OperationLog(name = "templList")
    @ApiOperation(value = "templList", response = String.class)
    public ApiResult<Paging<TemplVo>> templList(@Validated @RequestBody CommonPageParam commonPageParam) throws Exception {
        return ApiResult.ok(templService.getList(commonPageParam));
    }

    @RequestMapping(value = "/addTempl", method = RequestMethod.POST)
    @RequiresPermissions("/store/store/add")
    @OperationLog(name = "addTempl")
    @ApiOperation(value = "addTempl", response = String.class)
    public ApiResult<String> addTempl(String templData) throws Exception {
        String jsonText = StringEscapeUtils.unescapeHtml4(templData);
        TemplParam t = JSON.parseObject(jsonText, TemplParam.class);
        if(templService.add(t)) {
            return ApiResult.ok(null, "新增成功");
        }else{
            return ApiResult.fail("新增失败");
        }
    }

    @RequestMapping(value = "/saveEdit", method = RequestMethod.POST)
    @RequiresPermissions("/store/store/edit")
    @OperationLog(name = "saveEdit")
    @ApiOperation(value = "saveEdit", response = String.class)
    public ApiResult<String> saveEdit(String templData) throws Exception {
        String jsonText = StringEscapeUtils.unescapeHtml4(templData);
        TemplParam t = JSON.parseObject(jsonText, TemplParam.class);
        if(templService.edit(t)) {
            return ApiResult.ok(null, "修改成功");
        }else{
            return ApiResult.fail("修改失败");
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @RequiresPermissions("/store/store/delete")
    @OperationLog(name = "delete")
    @ApiOperation(value = "delete", response = String.class)
    public ApiResult<String> delete(Integer templId) throws Exception {
        if(templService.setDelete(templId)) {
            return ApiResult.ok(null, "删除成功");
        }else{
            return ApiResult.fail("删除失败");
        }
    }
}
